#include <54func.h>
typedef struct train_s{
    int length;
    char data[1000];
}train_t;
int main(int argc,char *argv[])
{
    // ./3_A 1.pipe file
    ARGS_CHECK(argc,3);
    int fdw_pipe = open(argv[1],O_WRONLY);
    ERROR_CHECK(fdw_pipe,-1,"open write");

    train_t train;//在栈上申请内存空间
    train.length = strlen(argv[2]);
    memcpy(train.data,argv[2],train.length);//将argv[2]这个文件长度为length的内容复制到data内
    //发送文件名
    //先发文件大小
    write(fdw_pipe,&train.length,sizeof(train.length));//文件名头
    //再发文件头内容
    write(fdw_pipe,train.data,train.length);
    

    //写文件内容
    //char buf[1024] = {0};
    int fdr_file = open(argv[2],O_RDONLY);
   // ssize_t sret = read(fdr_file,buf,sizeof(buf));
    ssize_t sret = read(fdr_file,train.data,sizeof(train.data));//直接把文件内容读取到小火车车厢内
    //先发送文件内容的大小
    train.length = sret;//因为文件的内容已经被读取到了buf缓存中，
   // memcpy(train.data,buf,sizeof(buf));
    //再发送文件内容
    write(fdw_pipe,&train.length,sizeof(train.length));//先发送文件头
    write(fdw_pipe,train.data,train.length);

    close(fdw_pipe);
    close(fdr_file);

    return 0;
}
